DOMAIN='http://你的域名' # HTTP协议配置的域名
WAN_IP='192.168.1.1' #服务器IP,用于生产git路径展示
WWW_USERNAME='www' # nginx实际操作用户
PRO_NAME=$1 #项目名称,执行脚本时传入
WEB_PATH='/www/wwwroot/' # WEB根目录
GIT_PATH='/www/gitstore/' # GIT裸仓库根目录
WEB_FULL_PATH=${WEB_PATH}${PRO_NAME}
echo "项目路径: ${WEB_FULL_PATH}"
GIT_FULL_PATH=${GIT_PATH}${PRO_NAME}.git
echo "Git路径: ${GIT_FULL_PATH}"
# 创建裸仓库,空仓库理论上来说最直接简单,但是远程提交时会出一系列问题,所以还是老老实实用裸的吧
git init --bare ${GIT_FULL_PATH}
# 修改钩子文件,在收到commit时同步代码
echo -e "git --work-tree=${WEB_FULL_PATH} checkout -f\nchown -R ${WWW_USERNAME}:${WWW_USERNAME} ${WEB_FULL_PATH}" > ${GIT_FULL_PATH}/hooks/post-receive
echo '钩子修改完成:'
cat ${GIT_FULL_PATH}/hooks/post-receive
echo "为钩子添加执行权限..."
chmod +x ${GIT_FULL_PATH}/hooks/post-receive # 为钩子添加执行权限
# 钩子完成了, 接下来要创建项目对应的WEB目录,并执行clone操作
echo "正在克隆项目..."
git clone ${GIT_FULL_PATH} ${WEB_FULL_PATH}
chown -R ${WWW_USERNAME}:${WWW_USERNAME} ${WEB_FULL_PATH} # 更改归属人为www
chown -R ${WWW_USERNAME}:${WWW_USERNAME} ${GIT_FULL_PATH} # 更改归属人为www
# 服务端Git配置完成, 此处的root为实际配置免密登录的账户名, 也就是配置完免密后可以免密登录服务器的账户名.
echo "仓库地址(SSH): root@${WAN_IP}:${GIT_FULL_PATH}"
# 使用HTTP协议需要自行配置
echo "仓库地址(HTTP): ${DOMAIN}/${PRO_NAME}.git"